package com.zige.zige.data;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.widget.ImageView;

public class ImageLoaderHelper {
	Context context;
	public ImageLoaderHelper(Context con) {
		context = con;
	}
	
	public void loadImage( ImageView imageView, String url, int defultRes){
		    RequestQueue mQueue = Volley.newRequestQueue(context);   
		          
		    ImageLoader imageLoader = new ImageLoader(mQueue, new BitmapCache());  
		  
		    ImageListener listener = ImageLoader.getImageListener(imageView, defultRes, defultRes);  
		    imageLoader.get(url, listener);  
		    //指定图片允许的最大宽度和高度  
		    //imageLoader.get("http://developer.android.com/images/home/aw_dac.png",listener, 200, 200);  
	}
	
	
	
	public class BitmapCache implements ImageCache {  
		  
	    private LruCache<String, Bitmap> cache;  
	  
	    public BitmapCache() {  
	        cache = new LruCache<String, Bitmap>(8 * 1024 * 1024) {  
	            @Override  
	            protected int sizeOf(String key, Bitmap bitmap) {  
	                return bitmap.getRowBytes() * bitmap.getHeight();  
	            }  
	        };  
	    }  
	  
	    @Override  
	    public Bitmap getBitmap(String url) {  
	        return cache.get(url);  
	    }  
	  
	    @Override  
	    public void putBitmap(String url, Bitmap bitmap) {  
	        cache.put(url, bitmap);  
	    }  
	}  
	
}
